package cn.ujn.medicalbackend.controller;

import cn.ujn.medicalbackend.common.BaseResponse;
import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.common.ResultUtils;
import cn.ujn.medicalbackend.domain.MedicalPolicy;

import cn.ujn.medicalbackend.dto.MedicalPolicyQuery;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.request.MedicalPolicyAddRequest;
import cn.ujn.medicalbackend.request.MedicalPolicyDeleteRequest;
import cn.ujn.medicalbackend.request.MedicalPolicyUpdateRequest;
import cn.ujn.medicalbackend.service.MedicalPolicyService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/medicalPolicy")
@CrossOrigin(origins = {"http://localhost:9092"})
public class MedicalPolicyController {
    @Autowired
    private MedicalPolicyService medicalPolicyService;

    @GetMapping("/list/medicalPolicy")
    public BaseResponse<Page<MedicalPolicy>> getmedicalPolicyListByPage(MedicalPolicyQuery medicalPolicyQuery) {
        if (medicalPolicyQuery== null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        MedicalPolicy medicalPolicy = new MedicalPolicy();
        BeanUtils.copyProperties(medicalPolicyQuery, medicalPolicy);
        QueryWrapper<MedicalPolicy> queryWrapper = new QueryWrapper<>(medicalPolicy);
        Page<MedicalPolicy> page = new Page<>(medicalPolicyQuery.getPageNum(), medicalPolicyQuery.getPageSize());
        Page<MedicalPolicy> tempPage = medicalPolicyService.page(page, queryWrapper);
        Page<MedicalPolicy> resultPage = medicalPolicyService.medicalPolicyPageTransfer(tempPage);
        return ResultUtils.success(resultPage);
    }
    @PostMapping("/add")
    public BaseResponse<Long> addMedicalPolicy(@RequestBody MedicalPolicyAddRequest medicalPolicyAddRequest, HttpServletRequest request) {
        if (medicalPolicyAddRequest== null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }

        long medicalPolicyId =medicalPolicyService.addMedicalPolicy(medicalPolicyAddRequest, request);
        return ResultUtils.success(medicalPolicyId);
    }

    @PostMapping("/update")
    private BaseResponse<Boolean> updateMedicalPolicy(@RequestBody MedicalPolicyUpdateRequest medicalPolicyUpdateRequest, HttpServletRequest request) {

        if (medicalPolicyUpdateRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        boolean result = medicalPolicyService.updateMedicalPolicy(medicalPolicyUpdateRequest,request);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    private BaseResponse<Boolean> deleteMedicalPolicy(@RequestBody MedicalPolicyDeleteRequest medicalPolicyDeleteRequest, HttpServletRequest request) {
        if (medicalPolicyDeleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result =  medicalPolicyService.deleteMedicalPolicy(medicalPolicyDeleteRequest, request);
        return ResultUtils.success(result);
    }
}
